package org.happen.android.core.net;

import org.happen.android.bean.BaseRequest;
import org.happen.android.bean.BaseResponse;
import org.happen.android.bean.response.FailResponse;
import org.happen.android.bean.response.JsonStrResponse;
import org.liushui.mycommons.android.log.McLog;

import com.google.gson.Gson;

public abstract class OnRequestCallback2 implements OnRequestCallback
{
	public void onSuccess(BaseRequest request, BaseResponse response)
	{
		JsonStrResponse jsonStrResponse = null;
		try
		{
			jsonStrResponse = new Gson().fromJson(response.json, JsonStrResponse.class);
			jsonStrResponse.json = response.json;
			jsonStrResponse.respId = response.respId;

			if (jsonStrResponse.isTokenInvalide())
			{
				onException(FailResponse.TOKEN_EX.exception);
			} else
			{
				onJsonResponse(request, jsonStrResponse);
			}
		} catch (Exception e)
		{
			McLog.e("", e);
			onException(FailResponse.JSON_EX.exception);
		}
	}

	/**
	 * 用于获取服务器返回的响应
	 * 
	 * @ReqParam request 请求
	 * @ReqParam jsonResponse json响应
	 */
	public abstract void onJsonResponse(BaseRequest request, JsonStrResponse jsonResponse);

	public void onException(Exception e)
	{
		McLog.m(this, "onException");
		McLog.e("", e);
		ReqRespUtils.showRespException(e);
	}
}